package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.data.Info;
	import com.rogo.rogobox.models.vo.TimelineVO;
	import com.rogo.rogobox.views.components.events.PageEvent;
	import com.rogo.rogobox.views.components.pages.PageHome;
	import com.rogo.rogobox.views.components.renderers.BlogStatusRenderer;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class PageHomeMediator extends Mediator
	{
		public function PageHomeMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'pageHomeMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var timelineVO:TimelineVO;
		private var updateBlogStatusArray:Array;
		private var refreshTimer:Timer;
		private var start:int = 0;
		private var end:int = 0;
		private var currentPage:int = 1;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():PageHome
		{
			return viewComponent as PageHome;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.FRIENDS_TIMELINE_RESULT,
				AppFacade.REPOST_MICROBLOG_RESULT,
				AppFacade.PUBLISH_MICROBLOG_RESULT];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case AppFacade.FRIENDS_TIMELINE_RESULT:
				{
					view.pageBar.enabled = true;
					updateBlogStatusArray = notification.param as Array;
					updateDisplay();
					break;
				}
				case AppFacade.REPOST_MICROBLOG_RESULT:
				case AppFacade.PUBLISH_MICROBLOG_RESULT:
				{
					refreshTimer_timerHandler(null);
					break;
				}
			}
		}
		
		override public function onRegister():void
		{
			// get default friends time line
			timelineVO = new TimelineVO();
			sendNotification(AppFacade.LOAD_FRIENDS_TIMELINE, timelineVO);
			
			// start auto refresh
			startTimer();
			
			view.pageBar.addEventListener(PageEvent.CHANGE, pageBar_changeHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function startTimer():void
		{
			refreshTimer = new Timer(App.REFRESH_INTERVAL);
			refreshTimer.addEventListener(TimerEvent.TIMER, refreshTimer_timerHandler);
			refreshTimer.start();
		}
		
		private function updateDisplay():void
		{
			if (!updateBlogStatusArray)
				return;
			
			updateBlogStatusArray.reverse();
			
			var blogStatusRenderer:BlogStatusRenderer;
			
			for each (var blogStatus:Info in updateBlogStatusArray)
			{
				if (view.contentList.numElements < App.PAGE_COUNT)
				{
					blogStatusRenderer = new BlogStatusRenderer();
					view.contentList.addElementAt(blogStatusRenderer, 0);
				}
				else
				{
					blogStatusRenderer = view.contentList.getElementAt(App.PAGE_COUNT - 1) as BlogStatusRenderer;
					view.contentList.setElementIndex(blogStatusRenderer, 0);
				}
				blogStatusRenderer.data = blogStatus;
			}
			
			var len:int = view.contentList.numElements;
			if (len> 0)
			{
				start = (view.contentList.getElementAt(0) as BlogStatusRenderer).data.timestamp;
				end = (view.contentList.getElementAt(len - 1) as BlogStatusRenderer).data.timestamp;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function refreshTimer_timerHandler(e:TimerEvent):void
		{
			// if is not the first page.  Not refresh
			if (view.pageBar.page != 1)   
				return;
			
			timelineVO.pageFlag = 2;
			timelineVO.pageTime = start;
			sendNotification(AppFacade.LOAD_FRIENDS_TIMELINE, timelineVO);
		}
		
		private function pageBar_changeHandler(e:PageEvent):void
		{
			view.pageBar.enabled = false;
			timelineVO.pageFlag = currentPage < e.page ? 1 : 2;
			timelineVO.pageTime = currentPage < e.page ? end : start;
			currentPage = e.page;
			sendNotification(AppFacade.LOAD_FRIENDS_TIMELINE, timelineVO);
		}
		
	}
}